package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.company.api.SsoApi;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.company.service.FaceService;
import cn.edu.fzu.homemaking.web.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/company")
@Api(tags = "登录相关模型")
public class LoginController {

    @Resource
    private SsoApi ssoApi;

    @Resource
    private FaceService faceService;

    @ApiOperation(value = "获取本人信息", httpMethod = "GET")
    @GetMapping("/auth/user/getInfo")
    public Result getInfo() {
        Result userInfo = ssoApi.getUserInfo();
        CompanyUser user = JSON.parseObject(((JSONObject) userInfo.getValue()).toJSONString(), CompanyUser.class);
        return new Result(user);
    }


    @ApiOperation(value = "修改本人信息", httpMethod = "POST")
    @PostMapping("/auth/user/updateInfo")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "address", value = "家庭地址"),
            @ApiImplicitParam(name = "faceUrl", value = "人脸照片"),
            @ApiImplicitParam(name = "email",value = "电子邮件") })
    public Result updateInfo(String email, String phone, String address, String faceUrl) {
        return ssoApi.updateUserInfo(phone, address, faceUrl,email);
    }


    @ApiOperation(value = "退出", httpMethod = "GET")
    @GetMapping("/logout")
    public Result logout() {
        return ssoApi.logout();
    }

    @ApiOperation(value = "上传人脸接口")
    @PostMapping("/auth/uploadFace")
    public Result upload(MultipartHttpServletRequest request) {
        return new Result(faceService.uploadFace(request.getFile("file")));
    }
}
